public class O68II {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        int max = Math.max(p.val, q.val), min = Math.min(p.val,q.val);
        while (true){
            if (root.val>=min&&root.val<=max)
                return root;
            else if (root.val>max)
                root = root.left;
            else
                root = root.right;
        }
    }
}